import {
  find,
  bargain
} from '../../../api/bargainPeriod';
const util = require('../../../utils/util.js')
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animationData: null,
    orderId:'1',
    bargainForm:{},
    bargainPeriod:{},
    widthbox:'',
    orderItem:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setNavSize();
    this.bindViewTap()
    this.data.orderId=options.id
    this.findClick( this.data.orderId)
  },


  bindViewTap: function() {
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })
    var next = true;
    setInterval(function () {
      if (next) {
        animation.scale(1.1,1.1).step()
        next = !next;
      } else {
        animation.scale(1,1).step()
        next = !next;
      }
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 500)
  },

  findClick(data) {
    var _this = this
    find({orderId:data}).then(
      res => {
        if (res.type == 'success') {
          console.log(res)
          _this.data.bargainForm = res.data.bargainBuy
          _this.data.bargainPeriod = res.data.bargainPeriod
          _this.data.orderItem = res.data.orderItem
          _this.data.widthbox = 630*(Number(_this.data.bargainPeriod.orderNum/_this.data.bargainPeriod.bargainNum).toFixed(2))
          console.log(_this.data.widthbox)
          _this.setData({
            bargainForm:_this.data.bargainForm,
            bargainPeriod:_this.data.bargainPeriod,
            widthbox:_this.data.widthbox,
            orderItem:_this.data.orderItem
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },


  bargainBotton(){
    var _this = this
    if (app.globalData.token == null) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      bargain({periodId:_this.data.bargainPeriod.id}).then(
        res => {
          if (res.type == 'success') {
            console.log(res)
            wx.showToast({
              title: '助力成功',
            })
            _this.findClick(_this.data.orderId)
          }
        }
      ).catch(error => {
        console.log(error)
      })
    }
  },

  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
      console.log(sysinfo)
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      status: statusHeight,
      navHeight: navHeight
    })
  },
  navTo(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})